package net.vanchu.mortgagecalculator.common.commonView.call;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.dtspread.apps.houseloan.R;

import net.vanchu.mortgagecalculator.common.CommonSoftKeyBoardHelper;
import net.vanchu.mortgagecalculator.common.commonView.component.WheelView;

import java.util.HashMap;
import java.util.List;

/**
 * PopupWindowHelper
 * <p/>
 * Created by Granton on 2016/12/16.
 */
public class PopupWindowHelper {

    private static PopWinWrapper currentPopWinWrapper;

    public static void closeAll() {
        if (currentPopWinWrapper == null) {
            return;
        }
        currentPopWinWrapper.dismiss();
    }

    private static void dismissPopupWindow(PopupWindow popupWindow) {
        if (popupWindow == null) {
            return;
        }
        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    }

    private static class CallBackImpl implements WheelView.OnItemSelectListener {

        String item;

        String getItem() {
            return item;
        }

        @Override
        public void onSelected(int selectedIndex, String item) {
            this.item = item;
        }
    }

    private interface PopWinWrapper {
        void show(View parentView);

        void dismiss();
    }

    private static class PopWinWrapperWithOneWv<T> implements PopWinWrapper {
        PopupWindow popupWindow;
        TextView left;
        TextView right;
        WheelView wv;
        View convertView;

        @Override
        public void show(View parentView) {
            CommonSoftKeyBoardHelper.checkSoftKeyBoard(parentView);
            popupWindow.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
        }

        @Override
        public void dismiss() {
            if (popupWindow == null) {
                return;
            }
            if (popupWindow.isShowing()) {
                popupWindow.dismiss();
            }
        }

        void bind(View parentView, int pos, List<String> optionKeys,
                  final HashMap<String, T> options,
                  final WheelView.SingleWvCallBack<T> singleWvCallBack) {

            if (convertView == null) {
                convertView = LayoutInflater.from(parentView.getContext()).inflate(
                        R.layout.layout_pop_win_with_one_wv, null);
                wv = (WheelView) convertView.findViewById(R.id.wv_single);
                wv.setOffset(3);
                left = (TextView) convertView.findViewById(R.id.tv_left);
                right = (TextView) convertView.findViewById(R.id.tv_right);
                popupWindow = new PopupWindow(convertView,
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT, true);
            }

            wv.setItems(optionKeys);
            final CallBackImpl callBack = new CallBackImpl();
            wv.setOnItemSelectListener(callBack);
            left.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismissPopupWindow(popupWindow);
                }
            });
            right.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismissPopupWindow(popupWindow);
                    T result = options.get(callBack.getItem());
                    singleWvCallBack.onSelected(result);
                }
            });
            wv.setSelection(pos);
        }
    }

    private static class PopWinWrapperWithTwoWv<LEFT, RIGHT> implements PopWinWrapper {

        PopupWindow popupWindow;
        TextView left;
        TextView right;
        WheelView wvLeft;
        WheelView wvRight;
        View convertView;

        @Override
        public void show(View parentView) {
            CommonSoftKeyBoardHelper.checkSoftKeyBoard(parentView);
            popupWindow.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
        }

        @Override
        public void dismiss() {
            if (popupWindow == null) {
                return;
            }
            if (popupWindow.isShowing()) {
                popupWindow.dismiss();
            }
        }

        void bind(View parentView, int leftPos, int rightPos,
                  List<String> optionKeysLeft, final List<String> optionKeysRight,
                  final HashMap<String, LEFT> optionsLeft, final HashMap<String, RIGHT> optionsRight,
                  final WheelView.TwoWvCallBack<LEFT, RIGHT> twoWvCallBack) {
            if (convertView == null) {
                convertView = LayoutInflater.from(parentView.getContext()).inflate(
                        R.layout.layout_pop_win_with_two_wv, null);
                wvLeft = (WheelView) convertView.findViewById(R.id.wv_left);
                wvLeft.setOffset(3);
                wvRight = (WheelView) convertView.findViewById(R.id.wv_right);
                wvRight.setOffset(3);
                left = (TextView) convertView.findViewById(R.id.tv_left);
                right = (TextView) convertView.findViewById(R.id.tv_right);
                popupWindow = new PopupWindow(convertView,
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT, true);
            }

            final CallBackImpl callBackLeft = new CallBackImpl();
            wvLeft.setItems(optionKeysLeft);
            wvLeft.setOnItemSelectListener(callBackLeft);

            final CallBackImpl callBackRight = new CallBackImpl();
            wvRight.setItems(optionKeysRight);
            wvRight.setOnItemSelectListener(callBackRight);

            left.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismissPopupWindow(popupWindow);
                }
            });
            right.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismissPopupWindow(popupWindow);
                    twoWvCallBack.onSelected(
                            optionsLeft.get(callBackLeft.getItem()),
                            optionsRight.get(callBackRight.getItem()));
                }
            });

            wvLeft.setSelection(leftPos);
            wvRight.setSelection(rightPos);
        }
    }

    public static <L, R> void callPopWinWithTwoWv(View parentView, int leftPos, int rightPos,
                                                  List<String> optionKeysLeft,
                                                  List<String> optionKeysRight,
                                                  HashMap<String, L> optionsLeft,
                                                  HashMap<String, R> optionsRight,
                                                  WheelView.TwoWvCallBack<L, R> twoWvCallBack) {

        PopWinWrapperWithTwoWv<L, R> popWinWrapperWithTwoWv = new PopWinWrapperWithTwoWv<>();
        popWinWrapperWithTwoWv.bind(parentView, leftPos, rightPos, optionKeysLeft, optionKeysRight,
                optionsLeft, optionsRight, twoWvCallBack);
        popWinWrapperWithTwoWv.show(parentView);
        currentPopWinWrapper = popWinWrapperWithTwoWv;
    }

    public static <T> void callPopWinWithSingleWv(View parentView, int pos,
                                                  final List<String> optionKeys,
                                                  final HashMap<String, T> options,
                                                  final WheelView.SingleWvCallBack<T> singleWvCallBack) {
        PopWinWrapperWithOneWv<T> popWinWrapperWithOneWv = new PopWinWrapperWithOneWv<>();
        popWinWrapperWithOneWv.bind(parentView, pos, optionKeys, options, singleWvCallBack);
        popWinWrapperWithOneWv.show(parentView);
        currentPopWinWrapper = popWinWrapperWithOneWv;
    }
}